package com.msmall.community.tag.bbs;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.exception.DataNotFoundException;
import com.msmall.base.pager.Messages;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.ApplicationManager;
import com.msmall.community.bbs.model.Master;
import com.msmall.community.bbs.service.MasterManager;
import com.msmall.community.common.OnlineUser;

//  设置精华,置顶, 推荐
public class MasterSetTag extends TagSupport {

	private static final long serialVersionUID = -9048958462410743857L;

	private static final Log logger = LogFactory.getLog(BbsTag.class);
	
	private String type;// value: elite; top; commend;  rmc(delete topic); rmd(delete thread)
	
	private String topicId;
	
	private String boardId;
	
	private String value; // 数据库存放的是否设置精华值
	
	public static final String MESSAGE_ELITE_SET = Messages.getMessage("common.bbs.topic.elite.set");
	public static final String MESSAGE_TOP_SET = Messages.getMessage("common.bbs.topic.top.set");
	public static final String MESSAGE_COMMEND_SET = Messages.getMessage("common.bbs.topic.commend.set");
	public static final String MESSAGE_ELITE_REMOVE = Messages.getMessage("common.bbs.topic.elite.remove");
	public static final String MESSAGE_TOP_REMOVE = Messages.getMessage("common.bbs.topic.top.remove");
	public static final String MESSAGE_COMMEND_REMOVE = Messages.getMessage("common.bbs.topic.commend.remove");
	public static final String MESSAGE_THREAD_REMOVE = Messages.getMessage("common.bbs.thread.remove");
	public static final String MESSAGE_TOPIC_REMOVE = Messages.getMessage("common.bbs.topic.remove");
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		String message = "";
		try {
			if(!isMaster()){
				return (SKIP_BODY);
			}
			if("elite".equalsIgnoreCase(type)){ // 精华
				if("1".equals(value)){// 已经设置了精华
					if("wml".equalsIgnoreCase(getUserAgentType())){
						message = getMessage(ConstantUrl.WML_ELITE_REMOVE,MESSAGE_ELITE_REMOVE,Constants.WML_NOTATION_AND);
					}else{
						message = getMessage(ConstantUrl.HTML_ELITE_REMOVE,MESSAGE_ELITE_REMOVE,Constants.HTML_NOTATION_AND);
					}
				}else{
					if("wml".equalsIgnoreCase(getUserAgentType())){
						message = getMessage(ConstantUrl.WML_ELITE_SET,MESSAGE_ELITE_SET,Constants.WML_NOTATION_AND);
					}else{
						message = getMessage(ConstantUrl.HTML_ELITE_SET,MESSAGE_ELITE_SET,Constants.HTML_NOTATION_AND);
					}
				}
			}
			if("top".equalsIgnoreCase(type)){ // 置顶
				if("1".equals(value)){// 已经设置了置顶
					if("wml".equalsIgnoreCase(getUserAgentType())){
						message = getMessage(ConstantUrl.WML_TOP_REMOVE,MESSAGE_TOP_REMOVE,Constants.WML_NOTATION_AND);
					}else{
						message = getMessage(ConstantUrl.HTML_TOP_REMOVE,MESSAGE_TOP_REMOVE,Constants.HTML_NOTATION_AND);
					}
				}else{
					if("wml".equalsIgnoreCase(getUserAgentType())){
						message = getMessage(ConstantUrl.WML_TOP_SET,MESSAGE_TOP_SET,Constants.WML_NOTATION_AND);
					}else{
						message = getMessage(ConstantUrl.HTML_TOP_SET,MESSAGE_TOP_SET,Constants.HTML_NOTATION_AND);
					}
				}
			}
			if("commend".equalsIgnoreCase(type)){ // 推荐
				if("1".equals(value)){// 已经设置了 推荐
					if("wml".equalsIgnoreCase(getUserAgentType())){
						message = getCommend(ConstantUrl.WML_COMMEND_REMOVE,MESSAGE_COMMEND_REMOVE,Constants.WML_NOTATION_AND);
					}else{
						message = getCommend(ConstantUrl.HTML_COMMEND_REMOVE,MESSAGE_COMMEND_REMOVE,Constants.HTML_NOTATION_AND);
					}
				}else{
					if("wml".equalsIgnoreCase(getUserAgentType())){
						message = getCommend(ConstantUrl.WML_COMMEND_SET,MESSAGE_COMMEND_SET,Constants.WML_NOTATION_AND);
					}else{
						message = getCommend(ConstantUrl.HTML_COMMEND_SET,MESSAGE_COMMEND_SET,Constants.HTML_NOTATION_AND);
					}
				}
			}
			if("rmc".equalsIgnoreCase(type)){ // remove topic
				if("wml".equalsIgnoreCase(getUserAgentType())){
					message = removeTopic(ConstantUrl.WML_TOPIC_REMOVE_MASTER,MESSAGE_TOPIC_REMOVE,Constants.WML_NOTATION_AND);
				}else{
					message = removeTopic(ConstantUrl.HTML_TOPIC_REMOVE_MASTER,MESSAGE_TOPIC_REMOVE,Constants.HTML_NOTATION_AND);
				}
			}
			if("rmd".equalsIgnoreCase(type)){// remove thread
				if("wml".equalsIgnoreCase(getUserAgentType())){
					message = removeThread(ConstantUrl.WML_THREAD_REMOVE_MASTER,MESSAGE_THREAD_REMOVE,Constants.WML_NOTATION_AND);
				}else{
					message = removeThread(ConstantUrl.HTML_THREAD_REMOVE_MASTER,MESSAGE_THREAD_REMOVE,Constants.HTML_NOTATION_AND);
				}
			}
			writer.print(message);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		return (SKIP_BODY);
	}
	
	private String removeThread(String url, String message, String and){
		String ctx = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		StringBuffer buffer = new StringBuffer();
		String path = ctx + url + "?tid=" + topicId + and + "hid=" + value;
		buffer.append("<a href=\"").append(path).append("\" >").append(message).append("</a>");
		return buffer.toString();
	}

	private String removeTopic(String url, String message, String and){
		String ctx = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		StringBuffer buffer = new StringBuffer();
		String path = ctx + url + "?tid=" + topicId + and + "bid=" + boardId;
		buffer.append("<a href=\"").append(path).append("\" >").append(message).append("</a>");
		return buffer.toString();
	}

	private String getCommend(String url, String message, String and){
		String ctx = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		StringBuffer buffer = new StringBuffer();
		String path = ctx + url + "?type=" + boardId + and + "eid=" + topicId;
		buffer.append("<a href=\"").append(path).append("\" >").append(message).append("</a>");
		return buffer.toString();
	}
	private String getMessage(String url, String message, String and){
		String ctx = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		StringBuffer buffer = new StringBuffer();
		String path = ctx + url + "?tid=" + topicId + and + "bid=" + boardId;
		buffer.append("<a href=\"").append(path).append("\" >").append(message).append("</a>");
		return buffer.toString();
	}
	
	private boolean  isMaster(){
		Long bid = Long.parseLong(boardId);
		MasterManager m = (MasterManager) ApplicationManager.appContext.getBean("masterManager");
		try {
			Master master = m.getMaster(bid, getUserid());
			if(master != null)
				return true;
			return false;
		} catch (DataNotFoundException e) {
			return false;
		}
	}
	
	private Long getUserid(){
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpSession session = request.getSession();
		Long userid = (Long) session.getAttribute(Constants.SESSION_USER_ID_KEY);
		return userid;
	}
	
	private String getUserAgentType(){
		try{
			return OnlineUser.getOnlineUsers().get(getUserid()).getUserAgentBean().getUserAgentType();
		}catch(Exception e){
			return Constants.USER_AGENT_TYPE_HTML;
		}
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTopicId() {
		return topicId;
	}

	public void setTopicId(String topicId) {
		this.topicId = topicId;
	}

	public String getBoardId() {
		return boardId;
	}

	public void setBoardId(String boardId) {
		this.boardId = boardId;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}

